package com.anyonavinfo.cpaddemo.view.mainview.miniradio;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.anyonavinfo.cpaddemo.data.hostinfo.source.service.E3HWServiceHelper;

import org.json.JSONException;
import org.json.JSONObject;





/**
 * Created by Drive on 2017/1/18.
 */

public class MiniRadioPresenter implements E3HWServiceHelper.ServiceObserver, MiniRadioContract.Presenter {
    private MiniRadioContract.View view;
    private E3HWServiceHelper serviceHelper;
    private String TAG = "RadioViewLog";
    private MiniRadioModel model;

    public MiniRadioPresenter(Context context) {
        serviceHelper = E3HWServiceHelper.getServiceHelperInstance();
        serviceHelper.registerObserver(this);
        model = new MiniRadioModel(context);
//        serviceHelper.ExecCommand(serviceHelper.INIT_RADIOINFO);//跟服务绑定时序有关
    }

    public void setRaidoView(MiniRadioContract.View view) {
        if (view == null) {
            throw new IllegalArgumentException("You can't set a null view on radioView !");
        }
        this.view = view;
        this.view.updateStationList(model.getBand(),model.getRadioStation());
    }

    @Override
    public void openApp(Context context) {
        Intent intent = context.getPackageManager()
                .getLaunchIntentForPackage("com.anyonavinfo.cpad.cpadfmradio");
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        }
    }


    @Override
    public void changeBand() {
        if (model.getBand() == 0) {
            serviceHelper.ExecCommand(serviceHelper.CHANGE_TO_FM_CHANNEL);
        } else if (model.getBand() == 1) {
            serviceHelper.ExecCommand(serviceHelper.CHANGE_TO_AM_CHANNEL);
        }
    }

    @Override
    public void playRadio() {
        if (model.getVoiceChannel() == 1) {
            serviceHelper.ExecCommand(serviceHelper.CHANGE_MEDIA_MODE);
        } else {
            serviceHelper.ExecCommand(serviceHelper.CHANGE_RADIO_MODE);
        }
    }

    @Override
    public void saveStation(int position) {
        if(model.getBand()==1){
            model.setRadioStation(position+3,model.getFmFrequency());
            model.saveStation(position+3,model.getFmFrequency());
            view.updateStation(position+3,model.getFmFrequency());
        }else if(model.getBand()==0){
            model.setRadioStation(position,model.getAmFrequency());
            model.saveStation(position,model.getAmFrequency());
            view.updateStation(position,model.getAmFrequency());
        }
    }

    @Override
    public void seekStation(int position) {
        if (model.getBand() == 0) {
            serviceHelper.ExecCommand(serviceHelper.SEEK_STATION, (model.getRadioStation())[position-1]);
        } else if (model.getBand() == 1) {
            serviceHelper.ExecCommand(serviceHelper.SEEK_STATION, (model.getRadioStation())[ position+2]);
        }
    }

    @Override
    public void seekUp() {
        serviceHelper.ExecCommand(serviceHelper.SEEK_UP);
    }

    @Override
    public void seekDown() {
        serviceHelper.ExecCommand(serviceHelper.SEEK_DOWN);
    }


    @Override
    public void update(String json) {
        String JsonID = "";
        JSONObject jsonparas;
        Log.e(TAG, "Received data from dispatcher :" + json);
        try {
            JSONObject jsonobject = new JSONObject(json);
            JsonID = jsonobject.names().getString(0);
            if (JsonID.endsWith("RADIOINFO")) {
                jsonparas = new JSONObject(jsonobject.getString("RADIOINFO"));
                model.setFmFrequency(Integer.parseInt(jsonparas.getString("FMFRQ")));
                model.setAmFrequency(Integer.parseInt(jsonparas.getString("AMFRQ")));
                model.setBand(Integer.parseInt(jsonparas.getString("BAND")));
                model.setVolume(Integer.parseInt(jsonparas.getString("VOL")));
                model.setMute(Integer.parseInt(jsonparas.getString("MUTE")));
                model.setVoiceChannel(Integer.parseInt(jsonparas.getString("VOLUMECHANNEL")));
                view.updateVoiceChannel(model.getVoiceChannel());
                if (model.getBand() == 0) {
                    int position=0;
                    for(int i=0;i<3;i++){
                        if(model.getAmFrequency()==(model.getRadioStation())[i]){
                            position = i+1;
                            break;
                        }
                    }
                    view.updateFrq(model.getBand(), model.getAmFrequency(),position,model.getRadioStation());
                } else if (model.getBand() == 1) {
                    int position=0;
                    for(int i=3;i<model.getRadioStation().length;i++){
                        if(model.getFmFrequency()==(model.getRadioStation())[i]){
                            position = i-2;
                            break;
                        }
                    }
                    view.updateFrq(model.getBand(), model.getFmFrequency(),position,model.getRadioStation());
                }

            } else if (JsonID.endsWith("FMSTATIONSLIST")) {
//                jsonparas = new JSONObject(jsonobject.getString("FMSTATIONSLIST"));
//                ArrayList<Integer> stationList = new ArrayList<Integer>();
//                ;
//                for (int i = 0; i < jsonparas.length(); i++) {
//                    stationList.add(Integer.parseInt(jsonparas.getString("station" + i)));
//                }
//                if (stationList.size() > 0) {
//                    view.updateStationList(1, stationList);
//                }

            } else if (JsonID.endsWith("AMSTATIONSLIST")) {
//                jsonparas = new JSONObject(jsonobject.getString("AMSTATIONSLIST"));
//                ArrayList<Integer> stationList = new ArrayList<Integer>();
//                ;
//                for (int i = 0; i < jsonparas.length(); i++) {
//                    stationList.add(Integer.parseInt(jsonparas.getString("station" + i)));
//                }
//                if (stationList.size() > 0) {
//                    view.updateStationList(0, stationList);
//                }

            } else if (JsonID.endsWith("ENFORCESTOPAUTOSEARCH")) {
//                radioHandler.sendEmptyMessage(SEARCH_ENFORCE_ENDED);
            } else if (JsonID.endsWith("ISPADCONNECT")) {
                jsonparas = new JSONObject(jsonobject.getString("ISPADCONNECT"));
                int isPadConnected = Integer.parseInt(jsonparas.getString("isPadConnect"));
//                Message msg = new Message();
//                msg.getData().putInt("isPadConnect", isPadConnected);
//                msg.what = IS_PAD_CONNECTED;
//                radioHandler.sendMessage(msg);
            } else if (JsonID.endsWith("ISPADRECONNECT")) {
//                Message msg = new Message();
//                msg.what = PAD_RECONNECTED;
//                radioHandler.sendMessage(msg);
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
